package cn.csust.net2.manager.client.panel;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import cn.csust.net2.manager.client.ux.ServiceCallback;
import cn.csust.net2.manager.shared.po.Authority;
import cn.csust.net2.manager.shared.po.Module;
import cn.csust.net2.manager.shared.po.Role;
import cn.csust.net2.manager.shared.po.User;
import cn.csust.net2.manager.shared.service.AuthorizationServiceAsync;
import cn.csust.net2.manager.shared.util.Utils;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.ListViewEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout.VBoxLayoutAlign;

/** 
 * @author 韩忠金
 * @version 创建时间：2012-2-7 下午03:19:31 
 * 类说明 
 */
public class SecurityWindow extends Window{
	private AuthorizationServiceAsync service;
	private User user;
	private ListView<BeanModel> listAll;
	private ListStore<BeanModel> storeAll;
	private ListView<BeanModel> listSelect;
	private ListStore<BeanModel> storeSelect;
	private Button add = new Button("增加");
	private Button delete= new Button("删除");
	private Html html = new Html();
	public SecurityWindow(User user,AuthorizationServiceAsync myService){
		super();
		this.service = myService;
		this.user =user;
		this.setWidth(400);
		this.setHeight(400);
		this.setLayoutOnChange(true);
		this.setLayout(new BorderLayout());
		
		loadRoleInfo();
		loadRolesInfo();
		dnd();
		loadHtmlInfo();
		this.show();
	}
	private void dnd(){
		
		add.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				MessageBox.confirm("警告", "你确定要增加所选权限吗?",new Listener<MessageBoxEvent>() {

					@Override
					public void handleEvent(MessageBoxEvent be) {
						
						Button btn = be.getButtonClicked();
						if (btn.getItemId().equals("yes")) {
							if(listAll.getSelectionModel().getSelectedItems().size()!=1){
								MessageBox.alert("提示", "请在右侧权限中选择一项", null);
								return;
							}
							BeanModel bean = listAll.getSelectionModel().getSelectedItem();
							Role r = bean.getBean();
							//增加操作
							addRole(r,user);
						} 
					}
				});
			
			}
		});
		delete.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				MessageBox.confirm("警告", "你确定要删除所选权限吗?",new Listener<MessageBoxEvent>() {

					@Override
					public void handleEvent(MessageBoxEvent be) {
						
						Button btn = be.getButtonClicked();
						if (btn.getItemId().equals("yes")) {
							if(listSelect.getSelectionModel().getSelectedItems().size()!=1){
								MessageBox.alert("提示", "请在左侧权限中选择一项", null);
								return;
							}
							BeanModel bean = listSelect.getSelectionModel().getSelectedItem();
							Role r = bean.getBean();
							//删除操作
							deleteRole(r,user);
						} 
					}
				});
				
			}
		});
		LayoutContainer ly = new LayoutContainer();
		ly.setLayout(new VBoxLayout(VBoxLayoutAlign.CENTER));
		ly.add(add , new VBoxLayoutData(new Margins(5, 5, 50, 5)));
		ly.add(delete , new VBoxLayoutData(new Margins(50, 5, 5, 5)));
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.CENTER, 150);  
		westData.setSplit(true);  
		westData.setCollapsible(true);  
		westData.setMargins(new Margins(10,0,0,0));  
		this.add(ly,westData);
	}
	private void addRole(Role role,User user){
		service.addRole(user.getUserID(), role.getRoleID(),new ServiceCallback<List<Role>>(){

			@Override
			public void onSuccess(List<Role> result) {
				reloadRoleInfo(result);
				
			}} );
	}
	private void deleteRole(Role role,User user){
		service.deleteRole(user.getUserID(), role.getRoleID(),new ServiceCallback<List<Role>>(){

			@Override
			public void onSuccess(List<Role> result) {
				reloadRoleInfo(result);
				
			}} );
	}
	private void loadHtmlInfo(){
		html.setStyleAttribute("overflow", "auto");
		listAll.addListener(Events.Select, new Listener<ListViewEvent<BeanModel>>() {

			@Override
			public void handleEvent(ListViewEvent<BeanModel> be) {
				//final Role role = list1.getSelectionModel().getSelectedItem().getBean();
				final Role role = be.getModel().getBean();
				service.loadAuthorityById(role.getRoleID(), new ServiceCallback<List<Authority>>() {
				
					@Override
					public void onSuccess(List<Authority> result) {
						StringBuffer bf = new StringBuffer(role.getRoleName()+"的权限为：</br>");
						Map<Module, List<Authority>> map= Utils.changeToMap(result);
						Set<Entry<Module, List<Authority>>> es = map.entrySet();
						for(Entry<Module, List<Authority>> entry:es){
							Module m = entry.getKey();
							 List<Authority> authoritys= entry.getValue();
							 bf.append(m.getDisplayName()+":");
							 bf.append("</br>");
							 for(Authority authority:authoritys){
								 bf.append("&nbsp&nbsp&nbsp"+authority.getDisplayName()+"</br>");
							 }
						}
						html.setHtml(bf.toString());
						
					}
				});
			}
		});
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.SOUTH, 150);  
		westData.setSplit(true);  
		westData.setCollapsible(true);  
		westData.setMargins(new Margins(10,0,0,0));  
		this.add(html,westData);
	}
	private void reloadRoleInfo(List<Role> result){
		BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory(Role.class);
		List<BeanModel> model = beanModelFactory.createModel(result);
		storeSelect.removeAll();
		storeSelect.add(model);
		listSelect.refresh();
	}
	private void loadRoleInfo(){
		//加载选择用户的权限
		listSelect = new ListView<BeanModel>();  
		listSelect.setDisplayProperty("roleName");
		storeSelect = new ListStore<BeanModel>();  
		listSelect.setStore(storeSelect);
		service.loadRole(user.getUserID(), new ServiceCallback<List<Role>>() {
			
			@Override
			public void onSuccess(List<Role> result) {
				// TODO Auto-generated method stub
				BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory(Role.class);
				List<BeanModel> model = beanModelFactory.createModel(result);
				storeSelect.add(model);
				BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 150);  
				westData.setSplit(true);  
				westData.setCollapsible(true);  
				westData.setMargins(new Margins(0,5,0,0));  
				SecurityWindow.this.add(listSelect,westData);
			}
		});
		
		
	}
	private void loadRolesInfo(){

		//加载所有权限
		listAll = new ListView<BeanModel>();  
		listAll.setDisplayProperty("roleName");
		storeAll = new ListStore<BeanModel>();  
		listAll.setStore(storeAll);
		service.loadRoles(new ServiceCallback<List<Role>>() {
		
		@Override
		public void onSuccess(List<Role> result) {
			// TODO Auto-generated method stub
			
			BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory(Role.class);
			List<BeanModel> model = beanModelFactory.createModel(result);
			storeAll.add(model);
			BorderLayoutData westData = new BorderLayoutData(LayoutRegion.EAST, 150);  
			westData.setSplit(true);  
			westData.setCollapsible(true);  
			westData.setMargins(new Margins(0,0,0,5));  
			SecurityWindow.this.add(listAll,westData);
			
		}
	}); 
	}
}
